package chen.dev.learn.aspect;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

/**
 * 服务运行时间统计切面
 *
 * @author chensj
 * @date 2020-04-20 21:17
 */
@Aspect
@Component
public class TimeConsumingStatisticsAspect {

    @Pointcut("execution(* chen.dev.learn.service..*.*(..))")
    public void consumingStatics(){}

    @Around("consumingStatics()")
    public Object methodTimeConsumingStatics(ProceedingJoinPoint pjp) throws Throwable {
        Long start = System.currentTimeMillis();
        Object ret = pjp.proceed();
        Long end = System.currentTimeMillis();
        long count = (end - start) /1000;
        System.out.println("记录：" + pjp.getTarget() + "." + pjp.getSignature() + " 耗时:" + count + "s");
        return ret;
    }
}
